package com.umanji.building.service;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.umanji.building.domain.BuildingInfos;
import com.umanji.building.domain.Buildings;
import com.umanji.building.domain.PublicOfficialBuildingBoards;
import com.umanji.building.domain.PublicOfficialBuildings;
import com.umanji.generic.service.GenericService;


@Service("PublicOfficialBuildingService")
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class PublicOfficialBuildingServiceImpl extends GenericService<PublicOfficialBuildings, Long> implements PublicOfficialBuildingService {

	private final Log log = LogFactory.getLog ( PublicOfficialBuildingServiceImpl.class );

    public PublicOfficialBuildings findByBuildingNo(String no) {
    	log.debug("building no ::::::::::::::::::::::::::::::: " + no);
    	// return (Tbbi001) em.createQuery("from " + Tbbi001.class.getSimpleName()).getSingleResult();
    	return (PublicOfficialBuildings) em.createQuery("Select t from " + PublicOfficialBuildings.class.getSimpleName() + " t where t.buildingNo = :no").setParameter("no", no).getSingleResult();
    }

    public PublicOfficialBuildings findByPobNo(String no) {
    	log.debug("public official building no ::::::::::::::::::::::::::::::: " + no);
    	// return (Tbbi001) em.createQuery("from " + Tbbi001.class.getSimpleName()).getSingleResult();
    	return (PublicOfficialBuildings) em.createQuery("Select t from " + PublicOfficialBuildings.class.getSimpleName() + " t where t.buildingNo = :no").setParameter("no", no).getSingleResult();
    }
    
    public List findByBuildingId(Long id, int firstResult, int maxResults){
    	return super.em.createQuery("Select t from " + PublicOfficialBuildingBoards.class.getSimpleName() + " t join t.publicOfficialBuilding b where b.id = :building_id").setParameter("building_id", id).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
    
    public void getBoardReadCount(Long boardId){
    	em.createQuery("update "+ PublicOfficialBuildingBoards.class.getSimpleName()+" b set b.readCount = b.readCount+1 where b.id = :boardId ").setParameter("boardId", boardId);
    }
    
    public PublicOfficialBuildingBoards findByBoardId(Long boardId){
    	return (PublicOfficialBuildingBoards)em.createQuery("select t from "+PublicOfficialBuildingBoards.class.getSimpleName()+" t where t.id = :boardId ").setParameter("boardId", boardId).getSingleResult();
    }
}
